/*
Implement a circular queue using arrays.

Program by Thomas Jacob & Syam Kumar
12 February,2001
*/


#include<stdio.h>
#include<conio.h>

int rear= 0,front=0, q[5], n=5;
char c;

void add()
{
 int item;

 if ((rear+1) % (n+1) == front)
  {printf("\nQueue full.\n");getch();}
 else
  {
     printf("\nNumber: "); scanf("%d",&item);
     q[rear]=item;
     rear= (rear+1) % (n+1);
     printf("Continue<Y/N>? "); c=toupper(getche());
     if(c!='N') add();
  };
}

void view()
{
 int i=front;

 printf("\n");
 if(rear==front) {printf("Queue empty."); getch(); }
 while((i % (n+1) != rear))
 {
  printf("\t %d",q[i]);
  i=(i+1) % (n+1);
 }
 getch();
}

void delete()
{
 if (front==rear) { printf("\nQueue empty."); getch(); }
 else
  {
   printf("\nElement %d deleted from queue.",q[front]);
   front=(front + 1) % (n);
   getch();
  }
}

void main()
{
 do
 {
  clrscr();
  printf("\n ------CIRCULAR QUEUE------\n");
  printf("<A> . . . Add \n");
  printf("<D> . . . Delete \n");
  printf("<V> . . . View \n\n");
  printf("<X> . . . EXIT \n\n");
  printf("Front : %d   Rear : %d\n\n",front,rear);
  printf("Choice : ");
  c=toupper(getche());

  switch(c)
   {
   case 'A': add(); break;
   case 'D': delete(); break;
   case 'V': view(); break;
   }


 } while(c!='X');}
